package com.kawaiim.zhj.GameProcess;


import com.kawaiim.zhj.LandlordActivity;

import java.util.Objects;

public class GameProcess {

    public SingleStartGame startGameProcess;  //准备阶段的流程
    public DealCardProcess dealCardProcess;        //发牌阶段的流程
    public OutCardProcess outCardProcess;      //出牌阶段的流程
    public ScoreSettleProcess scoreSettleProcess;      //结算积分的流程
    public EndGameProcess endGameProcess;      //结束游戏的流程

    //初始化需要操作的卡牌
    public GameProcess(LandlordActivity game) {
        //获取当前游戏单例
        startGameProcess = new SingleStartGame(game);
        outCardProcess = new OutCardProcess(game);


        //初始流程的类
        dealCardProcess = new DealCardProcess(game);
        scoreSettleProcess = new ScoreSettleProcess(game);
        endGameProcess = new EndGameProcess(game);
    }

    //当前状态的步骤
    public GameProcessEnum step = GameProcessEnum.PREPARE_GAME_PROCESS;
    //下一阶段的状态步骤
    public GameProcessEnum next_step = GameProcessEnum.NONE;


    //游戏进程状态转移(网络模式开始是从CallScoreDeal获取第一个叫分的人开始的)
    public void gameProcessChange() {
        switch (step) {

            //游戏准备开始阶段
            case PREPARE_GAME_PROCESS: {
                next_step = GameProcessEnum.DEAL_CARD_PROCESS;
            }
            break;

            //出牌阶段的流程（发完牌自动跳转）
            case DEAL_CARD_PROCESS: {
                //如果处于断线重连出牌阶段的模式，直接跳到出牌阶段

                    next_step = GameProcessEnum.OUT_CARD_PROCESS;

            }
            break;


            //出牌阶段的流程
            case OUT_CARD_PROCESS: {
                next_step = GameProcessEnum.SCORE_SETTLE_PROCESS;
            }
            break;

            //积分结算阶段的流程
            case SCORE_SETTLE_PROCESS: {
                next_step = GameProcessEnum.End_GAME_PROCESS;
            }
            break;

            //游戏结束流程
            case End_GAME_PROCESS: {
                next_step = GameProcessEnum.PREPARE_GAME_PROCESS;
            }
            break;
            default:
                throw new IllegalStateException("Unexpected value: " + step);
        }

        //开始初始化状态的转移
        initGameProcessUpdate();
    }

    //游戏进程转换前的初始化
    private void initGameProcessUpdate() {
        //跳转到下一个阶段前的初始化
        if (next_step == GameProcessEnum.NONE) {
            return ;
        }

        //发牌前进行初始化
        //throw new IllegalStateException("Unexpected value: " + next_step);
        if (Objects.requireNonNull(next_step) == GameProcessEnum.DEAL_CARD_PROCESS) {
            dealCardProcess.initDealCardAnimator();
            //todo
           /* case CALL_SCORE_PROCESS: case OUT_CARD_PROCESS: case SCORE_SETTLE_PROCESS: case End_GAME_PROCESS: case PREPARE_GAME_PROCESS:{

            }
            break;*/
        }

        step = next_step;

        //进行下一步
        gameProcessUpdate();
    }

    //游戏进程的状态转换
    private void gameProcessUpdate() {
        switch (step) {

            //发牌阶段的流程
            case DEAL_CARD_PROCESS: {
                dealCardProcess.startDealCard();
            }
            break;

            //出牌阶段的流程
            case OUT_CARD_PROCESS: {
                outCardProcess.startOutCard();
            }
            break;

            //积分结算阶段的流程
            case SCORE_SETTLE_PROCESS: {
                scoreSettleProcess.startSettleScore();
            }
            break;

            //游戏结束的流程
            case End_GAME_PROCESS: {
                //将当前状态置为false
                endGameProcess.startEndGame();
            }
            break;
        }
    }
}
